window.onload = function () {
    const origin = `${window.location.protocol}//${window.location.hostname}`;
    // const origin = 'https://xxdt.dev.ganguomob.com';

    let u = navigator.userAgent;
    let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android or uc
    let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

    const url = `${origin}/api/`;

    function parseQueryString() {
        var str = window.location.search.slice(1);
        var arr = str.split('&');
        var obj = {};
    
        for (var i = 0; i < arr.length; i++) {
            // 一般参数字符串都经过编码，使用decodeURIComponent()方法将键和值转为原始值
            var item = arr[i].split('=');
            var key = decodeURIComponent(item[0]);
            var value = decodeURIComponent(item[1]);
            obj[key] = value;
        }
    
        return obj;
    }

    const avatar = parseQueryString().avatar || './img/logo.png';
    const inviteId = parseQueryString().id || '';
    const userName = parseQueryString().name || '';
    
    new Vue({
        el: '#vm',
        data: {
            data: '',
            email: '',
            code: '',
            time: 0,
            avatar,
            inviteId,
            userName,
            loading: false,
            show: false,
        },
        methods: {
            fetchData() {
                axios.get(`${url}supplier/section/${id}`)
                    .then((res)=> {});
            },
            // 注册
            onRegister() {
                if (this.loading) return;
                this.loading = true;
                axios.post(`${url}auth/register`, {
                    email: this.email,
                    invite_id: this.inviteId,
                }).then(res => {
                    if (!res.data.code) {
                        this.show = true;
                    } else Toast(res.data.message, 1500);
                    this.loading = false;
                }).catch((error)=> {
                    Toast(error.data.message, 1500);
                    this.loading = false;
                });
            },
            // 验证邮箱
            checkEmail() {
                const data = this.emptyTip();
                if (!data.status) return Toast(data.msg, 1500);
                axios.get(`${url}supplier/email/check`, {
                    params: {
                        email: this.email,
                    },
                }).then(res => {
                    if (!res.data.data) {
                        this.sendCode();
                    } else Toast('该邮箱已注册', 1500);
                }).catch((error)=> Toast(error.data.message, 1500));
            },
            // 发送
            sendCode() {
                const number = Date.parse(new Date()) / 1000;
                localStorage.setItem('countdown', number);
                this.time = 30;
                this.countDown();
                axios.get(`${url}supplier/email`, {
                    params: {
                        email: this.email,
                        action: 'register',
                    },
                }).then(res => {});
            },
            // 验证code
            checkCode() {
                const data = this.emptyTip();
                if (!this.code) return Toast('请输入验证码', 1500);
                if (!data.status) return Toast(data.msg, 1500);
                axios.post(`${url}supplier/email`, {
                    email: this.email,
                    action: 'register',
                    code: this.code,
                }).then(res => {
                    if (!res.data.code) {
                        this.onRegister();
                    } else Toast(res.data.message, 1500);
                }).catch((error)=> Toast(error.data.message, 300));
            },
            // tip
            emptyTip() {
                var reg = new RegExp(/^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/);
                const email = this.email;
                const form = { status: true };
                if (!email) return { msg: '请输入邮箱', status: false };
                if (!reg.test(email)) return { msg: '请输入正确的邮箱格式', status: false };
                return form;
            },
            countDown() {
                const that = this;
                const timer = setInterval(() => {
                    that.time--;
                    if (!that.time) clearInterval(timer);
                }, 1000);
            },
            onConfirm() {
                window.location.href = './register.html';
            },
        },
        mounted() {
            const count = localStorage.getItem('countdown');

            if (count) {
                const number = Date.parse(new Date()) / 1000;
                if (number - (count * 1) < 30) {
                    this.time = 30 - (number - (count * 1));
                    this.countDown();
                }
            }

            wxConfig(`${url}supplier/wechat/config?url=${encodeURIComponent(location.href)}`,() => {
                setShare(
                    [
                        'menuItem:share:appMessage',
                        'menuItem:share:timeline',
                        'menuItem:share:qq',
                        'menuItem:share:QZone'
                    ],
                    {
                        link: location.href,
                        imgUrl: `${origin}/admin/h5/img/logo.png`,
                        title: '推荐你使用『角色地图App』系统学习延发技能，7天免费。',
                        desc: '围绕一个个典型工作场景，听专家分享研发经验，真的不错。',
                    },
                );
            });
        },
        created() {
            // this.fetchData();
        },
    });

    function openAPP() {
        var iOSUrl = "xxdt://course";//填URL Schemes
        var AndroidUrl = "xxdt://course";//填URL Schemes
        new Link({
            button: document.getElementById('t-button'),// 按钮
            androidLink: AndroidUrl,// 安卓的打开链接
            androidDownloadUrl: "//www.msup.com.cn/lm/default/app",// 安卓的下载链接
            androidYyb: false,// android 是否开启应用宝下载
            iosLink: iOSUrl,// ios 打开链接
            ios9Link: '//oia.msup.com.cn/',// ios 的Universal Link
            iosDownloadUrl: "//apps.apple.com/cn/app/id1509656761", // ios 的下载链接
            iosUniversalLinkEnabled: true, // 是否开启 Universal Link
            iosYyb: false,// ios 是否开启应用宝下载
            // yybDownloadUrl: "http://a.app.qq.com/o/simple.jsp?pkgname=com.luojilab.player&ckey=CK1369314671247",// 应用宝下载链接
            autoLaunchApp: false, // 是否打开页面就唤起APP
            autoRedirectToDownloadUrl: true,// 是否自动跳转到下载页面
        })
    }
    openAPP();
}
